package com.hll.service;

import com.hll.dao.UserDao;
import com.hll.exception.ParameterException;
import com.hll.exception.ServiceException;
import com.hll.model.User;
import com.hll.util.StringUtil;

public class UserService {
    //判断登录
    public User login(String userName, String password) throws ParameterException, ServiceException {
        //验证数据是否为空
        ParameterException parameterException = new ParameterException();

        if (StringUtil.isEmpty(userName)) {
            parameterException.addErrorField("userName", "User Name is required");
        }
        if (StringUtil.isEmpty(password)) {
            parameterException.addErrorField("password", "Password is required");
        }
        if (parameterException.isErrorField()) {
            throw parameterException;
        }

        UserDao userdao = new UserDao();
        User user = userdao.getUserByName(userName);

        if (user == null) {
            throw new ServiceException(1000, "用户不存在");
        }

        if (!password.equals(user.getPassword())) {
            throw new ServiceException(1001, "密码不正确");
        }
        return user;
    }
}
